using Digitalmes.Application.Mes.Masters.Materials;
using Digitalmes.Application.Mes.Masters.Materials.Commands;
using Digitalmes.Application.Mes.Masters.Materials.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Masters;

/// <summary>
/// 物料终结点。
/// </summary>
public sealed class MaterialEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/master", "material")
            .RequireAuthorization()
            .MapGet(GetMaterialListAsync, "list")
            .MapGet(GetMaterialPagedListAsync, "list-page")
            .MapPost(CreateMaterialAsync)
            .MapPut(UpdateMaterialAsync, "{id}")
            .MapDelete(DeleteMaterialAsync, "{id}");
    }

    [EndpointDescription("查询物料")]
    public async Task<IApiResult> GetMaterialListAsync(ISender sender, [AsParameters] GetMaterialListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询物料")]
    public async Task<IApiResult> GetMaterialPagedListAsync(ISender sender, [AsParameters] GetMaterialListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建物料")]
    public async Task<IApiResult> CreateMaterialAsync(ISender sender, MdMaterialDto dto)
    {
        var result = await sender.Send(new CreateMaterialCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新物料")]
    public async Task<IApiResult> UpdateMaterialAsync(ISender sender, long id, MdMaterialDto dto)
    {
        var result = await sender.Send(new UpdateMaterialCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除物料")]
    public async Task<IApiResult> DeleteMaterialAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteMaterialCommand(id));
        return result.ToApiResult();
    }
}
